//
//  SubView.m
//  calad
//
//  Created by KWSD on 15/10/12.
//  Copyright © 2015年 KWSD. All rights reserved.
//

#import "SubView.h"
#import "UILabel+StringFrame.h"
#import "UIColor+CL.h"
#import "Masonry.h"
@implementation SubView
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self createUI];
        _framey = frame.size.height-2;
//        CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
//        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.1; // 0.5 to 1.0,away from white
//        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.9; //0.5 to 1.0,away from black
//        self.backgroundColor =  [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
        self.backgroundColor =[UIColor colorWithHex:0x20a4e5];
//        self.layer.cornerRadius = 4;
//        self.layer.masksToBounds = YES;
    }
    return self;
}

- (float)getIOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] floatValue];
}

-(void)makeBackgroundColor:(NSInteger)colorNUm{
    self.backgroundColor =[UIColor colorWithHex:colorNUm];
}

-(void)createUI{
    NSInteger KWIDTH = [UIScreen mainScreen].bounds.size.width;
    _home =[[UILabel alloc]init];
    [self addSubview:_home];
    [_home sizeToFit];
    [_home mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(4);
        make.right.equalTo(self.mas_right).offset(-4);
        make.centerY.equalTo(self.mas_centerY);
    }];
//    _home.numberOfLines=0;
    if (KWIDTH<390) {
        _home.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
    }else{
        _home.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
    }
    
    _home.numberOfLines = 5;
    _home.lineBreakMode = NSLineBreakByTruncatingTail;
    _home.textColor =[UIColor whiteColor];
    _home.textAlignment = NSTextAlignmentLeft;
    
    float device =[self getIOSVersion];
    
    
    int fontNum ;
    if(device>9||device==9){
        if (KWIDTH<390) {
            fontNum=5;
        }else{
            fontNum=10;
        }
        
    }
    else{
        if (KWIDTH<390) {
            fontNum=6;
        }else{
            fontNum=11;
        }
    }
    
    _state =[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-10, 0, 10, 10)];
    [self addSubview:_state];
    
    _state.font =[UIFont systemFontOfSize:fontNum];

    _state.textColor =[UIColor whiteColor];
    _subTitle =[[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-20 ,self.frame.size.width, 10)];
    [self addSubview:_subTitle];
    _subTitle.textAlignment = NSTextAlignmentCenter;
    _subTitle.numberOfLines = 0;
    if (KWIDTH<390) {
        _subTitle.font = [UIFont systemFontOfSize:6];
    }else{
        _subTitle.font = [UIFont systemFontOfSize:11];
    }
    
    _subTitle.text =@"未知";
    _subTitle.textColor =[UIColor colorWithRed:55.0/255 green:104.0/255 blue:203.0/255 alpha:0.6];
    
    
    _englishView =[[UIView alloc]initWithFrame:CGRectMake(4, 20, self.frame.size.width-8, 30)];
    [self addSubview:_englishView];
    _englishView.hidden = YES;
//
//    CGSize size = [_subTitle boundingRectWithSize:CGSizeMake(self.frame.size.width, 0)];
//    _subTitle.frame = CGRectMake(2, 70, self.frame.size.width, size.height+20);
//    [_subTitle sizeToFit];
}

-(void)setEnglishArr:(NSArray *)array{
    
    _home.hidden= YES;
    _englishView.hidden = NO;
    NSInteger height = array.count*10+7*(array.count-1);
    float centerH = self.frame.size.height;
    _englishView.frame = CGRectMake(4, centerH/(float)2-(height/2), self.frame.size.width-8, height);
    for(int i=0;i<array.count;i++){
        
        NSLog(@"%f",self.frame.size.width);
        int aa = 0;
        if(array.count!=1){
            aa = 1;
        }
        UILabel *la =[[UILabel alloc]initWithFrame:CGRectMake(0, i*10+7*aa, self.frame.size.width-4, 10)];
        la.textColor =[UIColor colorWithHex:0xffffff];
        
        la.font =[UIFont fontWithName:@"Helvetica" size:9];
        
        float device =self.frame.size.width;
        
        if(device<330){
            la.font =[UIFont fontWithName:@"Helvetica" size:8];
        }
        

        
        la.numberOfLines = 0;
        la.textAlignment = NSTextAlignmentLeft;
        la.text = array[i];
//        [self setLabelSpace:la withValue:array[i] withFont:[UIFont systemFontOfSize:7]];
         [_englishView addSubview:la];
    }

}

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    paraStyle.alignment = NSTextAlignmentCenter;
    paraStyle.maximumLineHeight = 0;//最大行高
    paraStyle.lineSpacing = 0; //设置行间距
    
    paraStyle.hyphenationFactor = 0.0;
    
    paraStyle.firstLineHeadIndent = 0.0;
    
    paraStyle.paragraphSpacingBefore = 0.0;
    
    paraStyle.headIndent = 0;
    
    paraStyle.tailIndent = 0;
    
    //设置字间距 NSKernAttributeName:@1.5f
    
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    
    label.attributedText = attributeStr;
    
}

@end
